{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c0273fb-8bf3-4f3f-acf2-32d6a24af876",
   "metadata": {},
   "outputs": [],
   "source": [
    "def num_to_chinese(number):\n",
    "    num_chars = \"零一二三四五六七八九\"\n",
    "    unit_chars = [\"\", \"十\", \"百\", \"千\", \"万\", \"十万\"]\n",
    "\n",
    "    num_str = str(number)[::-1]\n",
    "    result = \"\"\n",
    "    need_zero = False\n",
    "    last_unit_wan = False  # 用于标记上一位是否是“万”\n",
    "\n",
    "    for i, digit in enumerate(num_str):\n",
    "        digit = int(digit)\n",
    "        if digit == 0:\n",
    "            need_zero = True\n",
    "        else:\n",
    "            if need_zero:\n",
    "                result = \"零\" + result\n",
    "                need_zero = False\n",
    "            result = num_chars[digit] + unit_chars[i] + result\n",
    "            last_unit_wan = unit_chars[i] == \"万\"\n",
    "\n",
    "    if result.endswith(\"零\"):\n",
    "        result = result[:-1]\n",
    "\n",
    "    if \"十万\" in result and \"万\" in result:\n",
    "        result = result.replace(\"十万\", \"十\")\n",
    "\n",
    "    if result == \"十\":\n",
    "        result = \"一十\"\n",
    "\n",
    "    return result\n",
    "\n",
    "number = int(input(\"请输入一个数字：\"))\n",
    "chinese = num_to_chinese(number)\n",
    "print(f\"中文表示为：{chinese}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
